<template>
	<div class="padding-across-10" v-loading="loading" element-loading-text="拼命加载中">
		<headerOpreate style="width: 300px;float: left;" :opreate-datas="opreateDatas" v-on:executeAction="executeAction"></headerOpreate>
		<div class="block" style="text-align:right;width: 59%;float: right;padding-bottom: 2px">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40, 50]" :page-size="pageSize" layout="total, sizes,prev, pager, next, jumper" :total="totalSize">
			</el-pagination>
		</div>
		<el-table border style="width: 100%;" @selection-change="handleSelectionChange" :data="tableData">
			<el-table-column :show-overflow-tooltip="true" type="selection"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="描述" align="center" prop="Year"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="创建时间" align="center" prop="CreateTime"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" :label="$t('message.operation')" align="center">
				<template slot-scope="scope">
					<el-button size="small" type="primary" @click="goDetail(scope.row)"><i class="el-icon-information el-icon--left"></i>详情</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="block" style="text-align:right;margin-bottom:60px;padding-top: 2px">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40, 50]" :page-size="pageSize" layout="total, sizes,prev, pager, next, jumper" :total="totalSize">
			</el-pagination>
		</div>
	</div>
</template>
<script>
	import headerOpreate from '../../../../components/headerOpreate'
	import $ from 'jquery'
	import { GloabFun, HttpServer } from '../../../../assets/js/common.js'
	import Kalendae from 'Kalendae'
	var picker = null;
	export default {
		components: {
			headerOpreate
		},
		data() {
			return {
				loading: false,
				isReload: false,
				tableData: [],
				currentPage: 1,
				pageSize: 10,
				totalSize: 0,
				multipleSelection: [],
				opreateDatas: [{
						show: true,
						size: "small",
						type: 'primary',
						icon: 'el-icon-eleg-icon_refresh el-icon--left',
						name: '刷新',
						action: 'reloadDatas'
					},
					{
						show: true,
						type: "primary",
						size: "small",
						icon: "el-icon-plus",
						name: "添加",
						action: "add"
					},
					{
						show: true,
						type: "danger",
						size: "small",
						icon: "el-icon-delete2",
						name: "删除",
						action: "miltipleDelete"
					}
				]
			}
		},
		props: {
			templateObj: {
				type: Object,
				default: function() {
					return {};
				}
			}
		},
		methods: {
			executeAction(action) {
				if(action) {
					this[action]();
				}
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDatas();
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getDatas();
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			formateSelectionData() {
				var result = [],
					array = this.multipleSelection;
				for(var i = 0; i < array.length; i++) {
					result.push(array[i].HolidayId);
				}
				return result;
			},
			//前往添加页面
			add() {
				this.$emit('changeView', 'Add', {});
			},
			//前往详情页面
			goDetail(row){
				this.$emit('changeView','Edit',row);
			},
			//批量删除
			miltipleDelete() {
				if(this.formateSelectionData().length > 0) {
					this.$confirm('此操作将删除所选项，是否继续？', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						HttpServer.httpPost.call(this, '/Api/Holiday/Delete', {
							ids: this.formateSelectionData(),
							UserId: parseInt(GloabFun.getKeyId())
						}, function(response) {}, function(response) {
							if(response.data.code === 0) {
								this.getDatas();
								this.$notify({
									title: '成功',
									message: this.$t('message.bulkDeleteSuccess'),
									type: 'success',
									duration: 2500
								});
							} else {
								this.$notify.error({
									title: '错误',
									message: response.data.msg,
									duration: 2500
								});
							}
						});
					}).catch(() => {
						this.$notify.info({
							title: '消息',
							message: this.$t('message.cancleOperation'),
							duration: 2500
						})
					});
				} else {
					this.$notify({
						title: this.$t('message.warning'),
						message: '请选择要批量删除的项',
						type: 'warning',
						duration: 2500
					});
				}
			},
			//去掉时间中的T
			formateData(array) {
				for(var i = 0; i < array.length; i++) {
					var index1 = array[i].CreateTime.indexOf("T");
					array[i].CreateTime = array[i].CreateTime.substring(0, index1) + " " + array[i].CreateTime.substring(index1 + 1, index1 + 9);
				}
			},
			//刷新
			reloadDatas() {
				this.isReload = true;
				this.getDatas();
			},
			getDatas() {
				this.loading = true;
				HttpServer.httpGet.call(this, '/Api/Holiday/List?index='+this.currentPage+'&pagesize='+this.pageSize, function(response) {}, function(response) {
					// console.log(response)
					this.loading = false;
					if(response.data.code === 0) {
						this.formateData(response.data.data);
						this.tableData = response.data.data;
						this.totalSize = response.data.total;
						if(this.isReload) {
							this.$notify({
								title: '成功',
								message: this.$t('message.refreshSuccess'),
								type: 'success',
								duration: 2500
							});
							this.isReload = false;
						}
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
				})
			}
		},
		created() {
			// console.log(111,this.templateObj)
			this.getDatas();
		}
	}
</script>
<style scoped>
	@import "../../../../../node_modules/kalendae/build/kalendae.css"
</style>
<style scoped lang="less">
	.kalendae .k-separator {
		width: 10px !important;
	}
</style>